package activity;

import java.util.ArrayList;
import java.util.Map;
import models.Score;

import flyingpig.com.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import application.Globalstate;

public class MainActivity extends Activity implements OnClickListener{

	private static final String TAG = MainActivity.class.getSimpleName();

	private Button playButton;
	private Button highscoreButton;
	private Button instructionsButton;
	private Button aboutButton;
	private Button quitButton;
	private Button settingsButton;

//	private Button urlButton;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		playButton = (Button) findViewById(R.id.play_button);
		playButton.setOnClickListener(this);
		highscoreButton = (Button) findViewById(R.id.highscore_button);
		highscoreButton.setOnClickListener(this);
		instructionsButton = (Button) findViewById(R.id.instruction_button);
		instructionsButton.setOnClickListener(this);
		aboutButton = (Button) findViewById(R.id.about_button);
		aboutButton.setOnClickListener(this);
		settingsButton = (Button) findViewById(R.id.settings_button);
		settingsButton.setOnClickListener(this);
		quitButton = (Button) findViewById(R.id.quit_button);
		quitButton.setOnClickListener(this);
//		urlButton = (Button) findViewById(R.id.url_button);
//		urlButton.setOnClickListener(this);
		populateHighscoreList();
		Log.d(TAG, "MainActivity created");
	}

	private void populateHighscoreList() {
		SharedPreferences highScores = getSharedPreferences("name_score_list", Context.MODE_PRIVATE);
		Map<String, ?> highScoreMap = highScores.getAll();
		Globalstate gs = (Globalstate) getApplication();
		if(gs.getHighscoreList().isEmpty()){
			for (Map.Entry<String, ?> entry : highScoreMap.entrySet()) {
				Score score = new Score(entry.getKey());
				score.setScore(Integer.valueOf((String) entry.getValue()));
				gs.addHighscore(score);

			}
		}
	}
	private void saveHighscoreList(){
		Globalstate gs = (Globalstate) getApplication();
		ArrayList<Score> highScoreList = gs.getHighscoreList();
		SharedPreferences keyValues = getBaseContext().getSharedPreferences("name_score_list", Context.MODE_PRIVATE);
		SharedPreferences.Editor keyValuesEditor = keyValues.edit();
		for (Score score : highScoreList) {
			keyValuesEditor.putString(score.getUser(), String.valueOf(score.getScore()));
		}
		keyValuesEditor.commit();
	}

	@Override
	public void onClick(View v) {
		if(v.equals(playButton)){
			Log.d(TAG, "Starting GameActivity");
			Intent intent = new Intent(MainActivity.this, GameActivity.class);
			startActivity(intent);
		}
		else if(v.equals(highscoreButton)){
			Log.d(TAG, "Starting HighscoreActivity");
			Intent intent = new Intent(MainActivity.this, HighscoreActivity.class);
			startActivity(intent);
		}
		else if(v.equals(instructionsButton)){
			Log.d(TAG, "Starting InstructionsActivity");
			Intent intent = new Intent(MainActivity.this, InstructionsActivity.class);
			startActivity(intent);
		}
		else if(v.equals(aboutButton)){
			Log.d(TAG, "Starting AboutActivity");
			Intent intent = new Intent(MainActivity.this, AboutActivity.class);
			startActivity(intent);
		}
		else if(v.equals(settingsButton)){
			Log.d(TAG, "Starting SettingsActivity");
			Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
			startActivity(intent);
		}
		else if(v.equals(quitButton)){
			saveHighscoreList();
			Log.d(TAG, "Closing the MainActivity");
			finish();
		}
//		else if(v.equals(urlButton)){
//			Log.d(TAG, "Starting the WebviewActivity");
//			Intent intent = new Intent(MainActivity.this, WebActivity.class);
//			startActivity(intent);
//		}
	}

	@Override
	public void onBackPressed() {
		saveHighscoreList();
		Log.d(TAG, "Closing the MainActivity");
		finish();
	}


}
